package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.PageDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.query.PageQuery;
import com.sky.query.SetmealQuery;
import com.sky.service.DishService;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.mapper.SetmealMapper;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
* @author 马毅爽
* @description 针对表【setmeal(套餐)】的数据库操作Service实现
* @createDate 2023-11-06 19:31:40
*/
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal>
    implements SetmealService{

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishService dishService;

    @Override
    public PageDTO<SetmealVO> queryPage(SetmealQuery setmealQuery) {
        Page<SetmealVO> page = Page.of(setmealQuery.getPage(),setmealQuery.getPageSize());
        Page<SetmealVO> p = setmealMapper.queryPage(page,setmealQuery);
        return PageDTO.of(p);
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO,Setmeal.class);
        save(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        if (Objects.equals(status, StatusConstant.ENABLE)){
            List<Long> dishIds = setmealDishMapper.getDishIdBySetmealId(id);
            dishIds.forEach(dishId -> {
                if (Objects.equals(dishService.getById(dishId).getStatus(), StatusConstant.DISABLE)){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        updateById(setmeal);
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = BeanUtil.copyProperties(setmealDTO,Setmeal.class);
        updateById(setmeal);

        setmealDishMapper.removeBySetmealId(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public SetmealVO queryById(Long id) {
        Setmeal setmeal = getById(id);
        SetmealVO setmealVO = BeanUtil.copyProperties(setmeal,SetmealVO.class);

        List<SetmealDish> setmealDishes = setmealDishService.lambdaQuery()
                                                            .eq(SetmealDish::getSetmealId,id)
                                                            .list();
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //起售状态的套餐不能删除
        ids.forEach(id -> {
            if (Objects.equals(getById(id).getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        //删除后清除套餐菜品表中的数据
        removeBatchByIds(ids);
        setmealDishMapper.deleteBySetmealIds(ids);


    }

    @Override
    public List<Setmeal> listByCategoryId(Long categoryId) {
        return lambdaQuery()
                .eq(Setmeal::getCategoryId,categoryId)
                .eq(Setmeal::getStatus,StatusConstant.ENABLE)
                .list();
    }

    @Override
    public List<DishItemVO> queryDishById(Long id) {
        return setmealDishMapper.queryDishById(id);
    }
}




